package com.zf.zffencheng.strategy.impl;

import com.alibaba.fastjson.JSON;
import com.william.yun.yunliwuli.api.request.goods.Goods;
import com.zf.zffencheng.exception.ResultException;
import com.zf.zffencheng.model.pojo.*;
import com.zf.zffencheng.model.vo.DeviceVo;
import com.zf.zffencheng.service.*;
import com.zf.zffencheng.strategy.HardwarePartnerFactory;
import com.zf.zffencheng.strategy.HardwarePartnerStrategy;
import com.zf.zffencheng.util.api.YunliWuliService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * ClassName: YunLiWuLiHardwarePartnerStrategy <br/>
 * Description:  <br/>
 * date: 2021/5/31 15:56<br/>
 *
 * @author william <br/>
 */
@Component
public class YunLiWuLiHardwarePartnerStrategy implements HardwarePartnerStrategy {
    @Autowired
    private YunliWuliService yunliWuliService;
    @Autowired
    private NtDeviceService ntDeviceService;
    @Autowired
    private DeviceTemplateService deviceTemplateService;
    @Autowired
    private TenantPalaceRegionService tenantPalaceRegionService;
    @Autowired
    private HardwarePartnerService hardwarePartnerService;
    @Autowired
    private HardwarePartnerAccountService hardwarePartnerAccountService;
    @Autowired
    private DonationPlaceService donationPlaceService;

    @Override
    public void afterPropertiesSet() throws Exception {
        HardwarePartnerFactory.register(YUNLIWULI, this);
    }

    /**
     * 添加 云里雾里设备
     * @param ntAllDevice
     * @param deviceVo
     * @return
     */
    @Override
    public NtAllDevice save(NtAllDevice ntAllDevice, DeviceVo deviceVo) {

        //登录
        Integer apiId = ntAllDevice.getApiId();
        String token = yunliWuliService.login(apiId);
        check(ntAllDevice);
        Integer regionId = ntAllDevice.getRegionId();
        PalaceRegion palaceRegion = tenantPalaceRegionService.getById(regionId);
        Integer type = palaceRegion.getType();
        DeviceTemplate deviceTemplate = deviceTemplateService.getByVersionAndTypeAndTypeId(YUNLIWULI, type, ntAllDevice.getDeviceType());
        if (deviceTemplate == null ) {
            throw new ResultException(500, "不支持当前版本号");
        }
        HardwarePartner hardwarePartner = hardwarePartnerService.getAndVaild(apiId);
        HardwarePartnerAccount hardwarePartnerAccount = hardwarePartnerAccountService.getByHardwareId(hardwarePartner.getId());
        String storeUUID = hardwarePartnerAccount.getStoreNo();
        String deviceSn = ntAllDevice.getDeviceSn();
        String templateNo = deviceTemplate.getTemplateNo();
        //添加商品绑定
        yunliWuliService.addLabelAndBinding(deviceSn, YunliWuliService.LABEL_KRY, storeUUID, token);
        String location = deviceVo.getLng()+"行"+deviceVo.getLnt()+"列";

        String palace = deviceVo.getPalaceName() + deviceVo.getRegionName();
        Goods goods = yunliWuliService.addGoods(deviceSn, storeUUID, token, location, palace);
        //推送消息
        yunliWuliService.updatebinding(deviceSn, templateNo, storeUUID, token);
        ntAllDevice.setExpandInfo(JSON.toJSONString(goods));
        ntAllDevice.setEquipmentVersion(deviceTemplate.getVersion());
        return ntAllDevice;
    }


    private void check(NtAllDevice filter) {
        if (!StringUtils.hasLength(filter.getDeviceSn())) {
            throw new ResultException(400, "mac值未输入");
        }
        if (filter.getUsingTempletId() == null || filter.getUsingTempletId() == 0) {
            filter.setUsingTempletId(1);
        }
        // mac
        if (StringUtils.hasLength(filter.getDeviceSn())) {
            NtAllDevice ntAllDevice = ntDeviceService.getDeviceSnAndApiId(filter.getDeviceSn(), filter.getApiId());
            if (ntAllDevice != null) {
                throw new ResultException(500, "该设备已被存在");
            }
        }

    }

}
